package com.framework.util;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.RichTextString;

public class PoiUtil {

	/**
	 * Agrega una el valor en una una de las columnas de la fila
	 * */
	public static void agregarCelda(org.apache.poi.ss.usermodel.Row filaCol,
			int initCol, String nombre, String comentario, CellStyle styleHeader) {
		org.apache.poi.ss.usermodel.Cell celda = filaCol.createCell(initCol,
				org.apache.poi.ss.usermodel.Cell.CELL_TYPE_STRING);
		celda.setCellValue("" + nombre);
		celda.setCellStyle(styleHeader);
		if (comentario != null && !comentario.trim().isEmpty()) {
			agregarComentario(comentario, celda);
		}
	}

	private static void agregarComentario(String comentario, Cell celda) {
		Drawing drawing = celda.getSheet().createDrawingPatriarch();
		CreationHelper factory = celda.getSheet().getWorkbook()
				.getCreationHelper();
		// When the comment box is visible, have it show in a 1x3 space
		ClientAnchor anchor = factory.createClientAnchor();
		anchor.setCol1(celda.getColumnIndex());
		anchor.setCol2(celda.getColumnIndex() + 1);
		anchor.setRow1(celda.getRowIndex());
		anchor.setRow2(celda.getRowIndex() + 1);
		anchor.setDx1(100);
		anchor.setDx2(100);
		anchor.setDy1(100);
		anchor.setDy2(100);

		Comment comment = drawing.createCellComment(anchor);
		RichTextString str = factory.createRichTextString(comentario);
		comment.setString(str);
		comment.setAuthor("Healthmanager");
		celda.setCellComment(comment);
	}

}
